#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
# Copyright 2013 Antoine Drouin (poinix@gmail.com)
#
# This file is part of PAT.
#
#    PAT is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    PAT is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PAT.  If not, see <http://www.gnu.org/licenses/>.
#

import math
import numpy as np
import scipy
import scipy.integrate
import scipy.signal
import matplotlib.pyplot as plt
import control.matlab

import pat.dynamic_model as dm
import pat.autopilot     as ap
import pat.utils as pu

np.set_printoptions(precision=2, suppress=True, linewidth=200)

time = np.arange(0., 35., 0.01)
my_dm = dm.load(type="rotorcraft", variant="synth")
my_ap = ap.load(type="rotorcraft", variant="synth", _dm=my_dm, _time=time[0])
my_ap.set_cur_mode(1) # LawGo
my_ap.set_ref(my_ap.get_cur_mode().reference[1]) # RefGo2
X0 = my_dm.reset()
X1 = np.array(X0); X1[0]+=10
my_ap.set_param_ref(X1, t0=5)
print my_ap.get_cur_mode().name
print my_ap.ref.name
X = np.zeros((len(time), my_dm.sv_size)); X[0] = my_dm.reset(X0) 
U = np.zeros((len(time), my_dm.iv_size))
Xref = np.zeros((len(time), my_dm.sv_size))

for i in range(1,len(time)):
    U[i-1], Xref[i-1] = my_ap.run(time[i-1], X[i-1])
    X[i,:] = my_dm.run(time[i]-time[i-1], U[i-1])
U[-1], Xref[-1] = my_ap.run(time[-1], X[-1])

my_dm.plot_trajectory(time, X, U, Xref, window_title="bla")
plt.show()
